/**
 * @Author 韦建韩
 * @Date 2022/3/25
 */
package com.zczn.order_system.service.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zczn.order_system.constant.AdministratorsConstant;
import com.zczn.order_system.constant.ExceptionResponseConstant;
import com.zczn.order_system.constant.HttpCodeConstant;
import com.zczn.order_system.constant.UserConstant;
import com.zczn.order_system.entity.Administrators;
import com.zczn.order_system.entity.ObjectArg;
import com.zczn.order_system.entity.UserInfo;
import com.zczn.order_system.entity.dto.ResultDto;
import com.zczn.order_system.exception.CustomException;
import com.zczn.order_system.mapper.UserMapper;
import com.zczn.order_system.service.IService.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired(required = false)
    UserMapper userMapper;

    @Override
    public ResultDto getUserList(IPage page) {
        ResultDto dto = new ResultDto();
        IPage page1 = userMapper.selectPage(page, null);
        dto.setData(page1);
        return dto;
    }

    public ResultDto addUserInfo(UserInfo userInfo) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
        queryWrapper.eq(UserConstant.ACCOUNT, userInfo.getAccount());
        UserInfo userInfo1 = userMapper.selectOne(queryWrapper);
        if (Objects.nonNull(userInfo1)) {
            throw new CustomException(HttpCodeConstant.Field.Reject.code, ExceptionResponseConstant.USEREXISTED);
        }
        userMapper.insert(userInfo);
        return new ResultDto();
    }

    @Override
    public ResultDto deleteUserInfo(UserInfo userInfo) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq(UserConstant.ACCOUNT, userInfo.getAccount());
        userMapper.delete(wrapper);
        return new ResultDto();
    }

    @Override
    public ResultDto updateUserinfo(UserInfo userInfo) {
        userMapper.updateById(userInfo);
        return new ResultDto();
    }

    @Override
    public ResultDto recharge(ObjectArg arg) {
        Integer id =  arg.get(UserConstant.ID,Integer.class);
        BigDecimal amount = arg.get(UserConstant.AMOUNT, BigDecimal.class);
        if(Objects.isNull(amount)){
            throw new CustomException(HttpCodeConstant.Field.MethodDisabled.code,ExceptionResponseConstant.RECHARGEAMOUNTNOTNULL);
        }
        UserInfo userInfo = userMapper.selectByUserId(id);

        userInfo.setAccounBalance(userInfo.getAccounBalance().add(amount));
        userMapper.updateById(userInfo);
        return ResultDto.doCreate();
    }

    @Override
    public Object before(Object arg) {
        return null;
    }

    @Override
    public Object after(Object arg) {
        return null;
    }
}
